{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3658dd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import liepindata\n",
    "import json\n",
    "from requests_html import HTMLSession\n",
    "import time\n",
    "import random\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "# 未登录cookie（模拟浏览器）\n",
    "cookie_未登录 = \"__uuid=1695198074804.68; __gc_id=d29e93953f694cba8829a2a82793325b; _ga=GA1.1.1963051593.1695199004; need_bind_tel=false; new_user=false; imId=b5e1273d5cb093f4ceec2a49177ea464; imId_0=b5e1273d5cb093f4ceec2a49177ea464; imClientId=b5e1273d5cb093f4c2b89f04a110357d; imClientId_0=b5e1273d5cb093f4c2b89f04a110357d; c_flag=a72838d40c64dd08f5c5140054978407; XSRF-TOKEN=vLw-GqefSWiArOH8kCCUww; __tlog=1701256535214.22%7C00000000%7C00000000%7C00000000%7C00000000; Hm_lvt_a2647413544f5a04f00da7eee0d5e200=1698838423,1700046606,1701256535; _ga_54YTJKWN86=GS1.1.1701256534.8.1.1701257219.0.0.0; __session_seq=4; __uv_seq=4; Hm_lpvt_a2647413544f5a04f00da7eee0d5e200=1701257220; __tlg_event_seq=10\"\n",
    "\n",
    "# 准备headers和session\n",
    "def create_headers(cookie):\n",
    "\n",
    "    return liepindata.headers(cookie)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "session = HTMLSession()\n",
    "\n",
    "def crawl(城市,关键词,工作经验=\"\",公司规模=\"\",页码=\"\",登录cookie=cookie_未登录):\n",
    "    ## 综上所述\n",
    "    # crawler \n",
    "    crawl_liepin_data = []\n",
    "    print(\"登录cookie\",登录cookie)\n",
    "    # 1. 确定页码数量\n",
    "    payload = liepindata.request_payload(城市=城市,关键词=关键词,工作经验=工作经验,公司规模=公司规模)\n",
    "    r = session.post(\n",
    "                url = 'https://api-c.liepin.com/api/com.liepin.searchfront4c.pc-search-job',\n",
    "                data = json.dumps(payload), # POST请求带的参数\n",
    "                headers = create_headers(登录cookie)    # 模拟真实用户\n",
    "    )\n",
    "#     print(\"create_headers(登录cookie\",create_headers(登录cookie))\n",
    "#     print(r.json())\n",
    "    totalPage = r.json()['data']['pagination']['totalPage']\n",
    "\n",
    "    # 2. 翻页请求（按照上述获取到的页面数量进行翻页请求）\n",
    "    for i in range(totalPage):\n",
    "        wait_time = random.randint(3,10)\n",
    "        # 1. 翻页请求的准备工作\n",
    "        payload = liepindata.request_payload(城市=城市,关键词=关键词,工作经验=工作经验,公司规模=公司规模,页码=str(i))\n",
    "    #     print(payload)\n",
    "        # 2. 发起请求\n",
    "        r = session.post(\n",
    "                url = 'https://api-c.liepin.com/api/com.liepin.searchfront4c.pc-search-job',\n",
    "                data = json.dumps(payload), # POST请求带的参数\n",
    "                headers = create_headers(登录cookie)    # 模拟真实用户\n",
    "            )\n",
    "        print(\"这是第%s页，接下来将先等待%s秒...然后以继续抓取\"%(i+1,wait_time))\n",
    "    #     print(r.json())\n",
    "        r.json()\n",
    "        df = pd.json_normalize( r.json()['data']['data']['jobCardList'])\n",
    "        crawl_liepin_data.append(df)\n",
    "        # 3. 分批次发起等待时间\n",
    "\n",
    "        time.sleep(wait_time)\n",
    "    df = pd.concat(crawl_liepin_data)\n",
    "\n",
    "    return df\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
